/**
 * 用户表模型
 */
module.exports = (app) => {
  const { STRING, BIGINT, BOOLEAN, DATE, DOUBLE } = app.Sequelize
  const User = app.model.define('user', {
    id: {
      type: BIGINT(20),
      autoIncrement: true,
      primaryKey: true,
    },
    state: {
      type: BOOLEAN,
      allowNull: true,
    },
    name: {
      type: STRING,
      allowNull: true,
    },
    headImgUrl: {
      type: STRING,
      allowNull: true,
    },
    mobile: {
      type: STRING(11),
      allowNull: true,
    },
    salt: {
      type: STRING(64),
      allowNull: true,
    },
    password: {
      type: STRING(64),
      allowNull: true,
    },
    created: {
      type: DATE,
      allowNull: true,
      defaultValue: app.Sequelize.NOW
    },
    creator: {
      type: STRING(36),
      allowNull: true,
    },
    edited: {
      type: DATE,
      allowNull: true,
    },
    editor: {
      type: STRING(36),
      allowNull: true,
    },
    deleted: {
      type: DOUBLE,
      allowNull: true,
    },
  },
    {
      freezeTableName: true, // Model 对应的表名将与model名相同
      timestamps: false,
    })

  User.associate = function () {
    app.model.User.belongsToMany(app.model.Role, {
      through: app.model.UserRole,
      foreignKey: 'userId',
      otherKey: 'roleId',
    })
  }

  return User
}